package com.alen.sun.service.Impl;

import com.alen.sun.bean.Employee;
import com.alen.sun.bean.PageBean;
import com.alen.sun.dao.ElderlyMapper;
import com.alen.sun.dao.EmployeeMapper;
import com.alen.sun.dao.MessageMapper;
import com.alen.sun.service.EmployeeService;
import com.alen.sun.util.PhoneCode;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private ElderlyMapper elderlyMapper;

    @Override
    public Boolean add(Employee employee) {
        Optional<String>[] optionals = new Optional[]{
                Optional.ofNullable(employee.getName()),
                Optional.ofNullable(employee.getGender()),
                Optional.ofNullable(employee.getPhoneNum()),
                Optional.ofNullable(employee.getPassword())
        };

        if (Arrays.stream(optionals).allMatch(opt -> opt.isPresent() && !opt.get().isEmpty())) {
            //先判断该手机号是否已注册
            Employee e = employeeMapper.selectPhoneNum(employee);
            if (e == null) {
                //该手机号未注册
                employee.setEmployeeId(PhoneCode.generateCode());//设置护工ID为6位随机数
                //密码加密
                String encryptedPassword = DigestUtils.md5Hex(employee.getPassword());
                employee.setPassword(encryptedPassword);
                employeeMapper.add(employee);
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    @Override
    public Employee getById(String id) {
        Employee employee = employeeMapper.getById(id);
        return employee;
    }

    @Override
    public Boolean update(Employee employee) {
        if (employee.getPassword() != null) {
            employee.setPassword(DigestUtils.md5Hex(employee.getPassword()));
            Boolean flag = employeeMapper.update(employee);
            return flag;
        } else {
            Boolean flag = employeeMapper.update(employee);
            return flag;
        }
    }

    //获取未分配满4人老人的护工id
    @Override
    public List<String> getEmployeeId() {
        List<String> employeeIds = employeeMapper.getEmployeeId();
        return employeeIds;
    }

    @Override
    public Employee login(Employee employee) {
        if (employee.getPhoneNum() != null && !employee.getPhoneNum().isEmpty()
                && employee.getPassword() != null && !employee.getPassword().isEmpty()) {
            employee.setPassword(DigestUtils.md5Hex(employee.getPassword()));
            //数据库查找记录
            Employee e = employeeMapper.selectPhoneNum(employee);
            return e;
        } else {
            return null;
        }
    }

    @Override
    public Boolean delete(String id) {
        Boolean flag = employeeMapper.delete(id);
        return flag;
    }

//    @Override
//    public List<Employee> selectByWord(String word) {
//        word="%"+word+"%";
//        List<Employee> employees=employeeMapper.selectByWord(word);
//        return employees;
//    }


    @Override
    public Boolean deleteByIds(List<String> ids) {
        Boolean flag = employeeMapper.deleteByIds(ids);
        return flag;
    }

    @Override
    public PageBean page(Integer page, Integer pageSize, String word) {
        //1. 设置分页参数
        PageHelper.startPage(page, pageSize);
        List<Employee> employees;
        //2. 执行查询
        if (word == null || word.equals("")) {
            employees = employeeMapper.list();
        } else {
            word = "%" + word + "%";
            employees = employeeMapper.selectByWord(word);
        }
        Page<Employee> p = (Page<Employee>) employees;

        //3. 封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public Map<String, Object> BI() {
        List<Employee> employees = employeeMapper.list();
        List<String> names = new ArrayList<>();
        List<String> ids = new ArrayList<>();
        List<Integer> messageCount = new ArrayList<>();
        List<Double> standard = new ArrayList<>();
        for (Employee employee : employees) {
            names.add(employee.getName());
            ids.add(employee.getEmployeeId());
        }
        for (String id : ids) {
            int message = messageMapper.getCount(id);
            messageCount.add(message);
            int count = elderlyMapper.getCountByEmpId(id);

            // 添加判断条件，避免除零错误
            double value = count != 0 ? (double) (message / count) : 0.0;
            standard.add(value);
        }
        Map<String, Object> BI = new HashMap<>();
        BI.put("names", names);
        BI.put("messageCount", messageCount);
        BI.put("standard", standard);
        return BI;
    }
}
